package ru.housewife.form;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Form object for
 * @see ru.housewife.model.CookingDishRecipeGroup
 *
 * @author Alexander Troshanin
 */
public class CookingDishRecipeGroupForm implements Comparable<CookingDishRecipeGroupForm> {
    private long id;

    private String name;

    private CookingDishRecipeGroupForm parent;

    private List<CookingDishRecipeGroupForm> childs;

    private List<CookingDishRecipeForm> recipes; // here will be stored ONLY common recipes

    private boolean defaultGroup;

    private int order;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CookingDishRecipeGroupForm getParent() {
        return parent;
    }

    public void setParent(CookingDishRecipeGroupForm parent) {
        this.parent = parent;
    }

    public List<CookingDishRecipeGroupForm> getChilds() {
        return childs == null ? Collections.<CookingDishRecipeGroupForm>emptyList() : childs;
    }

    public void setChilds(List<CookingDishRecipeGroupForm> childs) {
        this.childs = childs;
    }

    public List<CookingDishRecipeForm> getRecipes() {
        return recipes == null ? Collections.<CookingDishRecipeForm>emptyList() : recipes;
    }

    public void setRecipes(List<CookingDishRecipeForm> recipes) {
        this.recipes = recipes;
    }

    public void addRecipe(CookingDishRecipeForm recipeForm) {
        if (recipes == null) {
            recipes = new ArrayList<CookingDishRecipeForm>();
        }
        recipes.add(recipeForm);
    }

    public boolean isDefaultGroup() {
        return defaultGroup;
    }

    public void setDefaultGroup(boolean defaultGroup) {
        this.defaultGroup = defaultGroup;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public List<CookingDishRecipeForm> getAllRecipes() {
        List<CookingDishRecipeForm> result = new ArrayList<CookingDishRecipeForm>();
        result.addAll(getRecipes());
        for (CookingDishRecipeGroupForm child : getChilds()) {
            result.addAll(child.getAllRecipes());
        }
        Collections.sort(result);
        return result;
    }

    @Override
    public int compareTo(CookingDishRecipeGroupForm that) {
        int thisVal = this.order;
        int thatVal = that.order;
        return (thisVal < thatVal ? -1 : (thisVal == thatVal ? 0 : 1));
    }
}
